<template>
  <div class="main">
    <div class="top">
      <div class="breadcrumb">
        <p class="toptitle">首页</p>
        <i class="iconfont icon-youjiantou"></i>
        <p class="toptitle">账号管理</p>
        <i class="iconfont icon-youjiantou"></i>
        <p class="lasttitle">添加账号</p>
      </div>
    <welcome></welcome>
    </div>
    <div class="table">
      <p class="list">添加账号</p>

      <!-- 表单 -->
      <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
        class="demo-ruleForm" :size="formSize" status-icon>
        <el-form-item label="账号" prop="account">
          <el-input v-model="ruleForm.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="用户组" prop="userGroup">
          <el-select v-model="ruleForm.userGroup" placeholder="请选择用户组">
            <el-option label="超级管理员" value="超级管理员" />
            <el-option label="普通用户" value="普通用户" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            立即添加
          </el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import type { ComponentSize, FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { addaccount } from '../../api/account';
import Welcome from '../../components/Welcome.vue';

// 定义表单数据接口
interface RuleForm {
  account: string; // 账号字段
  password: string; // 密码字段
  userGroup: string; // 用户组字段
}

// 表单大小
const formSize = ref<ComponentSize>('default');

// 表单引用
const ruleFormRef = ref<FormInstance>();

// 表单数据
const ruleForm = reactive<RuleForm>({
  account: '',
  password: '',
  userGroup: ''
});
// axios
const accountadd = async (data) => {
  let res = await addaccount(data);
  console.log(res);

}

// 表单验证规则
const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 5, message: '账号长度应为 3 到 5 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 5, message: '密码长度应为 3 到 5 个字符', trigger: 'blur' },
  ],
  userGroup: [
    { required: true, message: '请选择用户组', trigger: 'blur' },
  ],
});

// 提交表单
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 打印获取到的表单信息
      accountadd(ruleForm);
      console.log('提交的表单数据:', ruleForm.name);
      ElMessage.success('账号添加成功！');
      // 在这里可以调用 API 提交表单数据
    } else {
      console.log('error submit!', fields);
      ElMessage.error('账号添加失败，请检查输入内容！');
    }
  });
};

// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped>
/* 顶部面包屑 */
.toptitle {
  font-weight: bold;
}

.top {
    width: 96%;
    height: 50px;
    background-color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* 内容分布在左右两侧 */
    padding: 0 20px;
    /* 添加左右内边距 */
}

.iconfont {
  font-size: 14px;
  color: #999999;
  margin: 0 5px;
}

.breadcrumb {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.lasttitle {
  color: #999999;
}

.main {
  width: 100%;
  height: 730px;
  background-color: #f0f0f0;
}



.table {
  margin: 20px;
  padding: 20px;
  border-radius: 5px;
  background-color: #fff;
  min-height: 600px;
}

.list {
  width: 100%;
  border-bottom: 1px solid #e1e1e1;
  font-size: 20px;
  margin-bottom: 20px;
  padding-bottom: 10px;
}
</style>